package com.android.support.lib.universal.view.viewpager;

import java.util.ArrayList;
import java.util.List;

import com.android.support.lib.universal.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 自定义小圆点个数
 * Config.countDot=5;//设置小圆点的个数，这个必须在setContentView()方法之前才有效果
 * indicator = (CustomIndicator) findViewById(R.id.indicator);//小圆点
 * indicator.setCurrentPosition(arg0%Config.countDot);//设置当前选中的小圆点
 * 布局：必须在里面添加	xmlns:indic="http://schemas.android.com/apk/res-auto"
    <com.android.support.lib.universal.view.viewpager.CustomIndicator
    	xmlns:indic="http://schemas.android.com/apk/res-auto"
        android:id="@+id/indicator"
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_below="@+id/viewPager"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-27dp"
        indic:count="5"
        indic:heights="10dp"
        indic:margin="4dp"
        indic:normal_icon="@drawable/lib_dot_unselected"//正常圆点图片
        indic:selected_icon="@drawable/lib_dot_selected"//选择圆点图片
        indic:width="10dp" >
    </com.android.support.lib.universal.view.viewpager.CustomIndicator>
 * 
 * @author 彭治铭
 *
 */
public class CustomIndicator extends LinearLayout {
	/**
	 * 有对应的attrs.xml配置文件
	 */
	private Context mContext;
	private int width;
	private int height;
	private int margin;
	private Drawable normalDrawable, selectedDrawable;
	private int count = 0;// 自定义count的个数,个数只能在构造方法了一开始就赋值，其后不允许更改。
	private int currentCount = 0;
	private List<ImageView> views = new ArrayList<ImageView>();

	@SuppressLint("NewApi")
	public CustomIndicator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public CustomIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		TypedArray a = mContext.obtainStyledAttributes(attrs,
				R.styleable.CustomIndicator);
		margin = (int) a.getDimension(R.styleable.CustomIndicator_margin, 0);
		width = (int) a.getDimension(R.styleable.CustomIndicator_width, 0);
		height = (int) a.getDimension(R.styleable.CustomIndicator_heights, 0);
		count = Config.countDot;// 自定义count的个数,赋值必须在setContentView()方法之前才有效果
		normalDrawable = a.getDrawable(R.styleable.CustomIndicator_normal_icon);
		selectedDrawable = a
				.getDrawable(R.styleable.CustomIndicator_selected_icon);
		a.recycle();
		initViews();
	}

	public CustomIndicator(Context context) {
		super(context);
	}

	@SuppressLint("NewApi")
	public void setCurrentPosition(int pos) {
		currentCount = pos;
		if (currentCount < 0) {
			currentCount = 0;
		}
		if (currentCount > count - 1) {
			currentCount = count - 1;
		}

		for (int i = 0; i < count; i++) {
			views.get(i).setBackground(normalDrawable);
		}
		if (isInEditMode()) {
			return;
		}
		views.get(currentCount).setBackground(selectedDrawable);
		;
	}

	public void next() {
		setCurrentPosition(currentCount + 1);
	}

	public void previous() {
		setCurrentPosition(currentCount - 1);
	}

	public void setCount(int count) {
		this.count = count;
		this.currentCount = 0;
	}

	@SuppressLint("NewApi")
	private void initViews() {
		views.clear();
		for (int i = 0; i < count; i++) {
			ImageView view = new ImageView(mContext);
			views.add(view);
			LayoutParams params = new LayoutParams(
					width == 0 ? LayoutParams.WRAP_CONTENT : width,
					height == 0 ? LayoutParams.WRAP_CONTENT : height);
			if (i != count - 1) {
				params.rightMargin = margin;
			}
			view.setLayoutParams(params);
			view.setBackground(normalDrawable);
			this.addView(view);
		}
		setCurrentPosition(0);
	}

}
